import tkinter as tk
import sys
from pymysql import *
import tkinter.messagebox as mb
import index
# conn = connect(host='127.0.0.1', user = "root", passwd="eq10324", db="studentc", port=3306, charset="utf8")
conn = connect(host="localhost", user="root", password="eq10324", database="studentscore",charset="utf8")
cur = conn.cursor()



#登陆功能
def denglu():#user, password
    c1=v1.get()
    c2=v2.get()
    sql = "select * from yh where user='"+c1+"'"
    cur.execute(sql)
    us=cur.fetchall()
    sql = "select * from yh where user='" + c1 + "' and password='" + c2 + "'"
    cur.execute(sql)
    pw = cur.fetchall()
    #print(pw)
    #print('运行到这了',us,c1,c2)
    if len(us)==0:
        mb.showwarning('登录','用户不存在！')
    else:
        if len(pw)==0:
            mb.showwarning('登录', '密码错误！')
        else:
            mb.showwarning('登录', '登陆成功！')
            root1.destroy()
            # 主界面
            interface = index.maininterface()
            interface.index()
            # rt = tk.Tk()
            # rt.title('学生信息管理系统')
            # rt.geometry('550x350+250+200')
            # rt.iconbitmap('pro.ico')
            #
            # # 添加窗口背景图片
            # idat = tk.PhotoImage(file='image.gif')
            # lab1 = tk.Label(image=idat)
            # lab1.place(x=0, y=0)  # 打包显示
            #
            # # 创建顶级菜单和下拉菜单
            # menubar = tk.Menu(rt)
            # filemenu = tk.Menu(menubar, tearoff=0)  # tearoff:分窗
            # filemenu.add_command(label='增加记录', command=add)  # 添加命令
            # filemenu.add_command(label='删除记录', command=delete)
            # filemenu.add_command(label='修改记录', command=change)
            # filemenu.add_command(label='查询一个学生的记录', command=quire)
            # filemenu.add_command(label='浏览学生记录', command=query)
            # filemenu.add_separator()  # 添加分隔线
            # filemenu.add_command(label='退出', command=rt.destroy)
            # menubar.add_cascade(label='菜单', menu=filemenu)  # 添加子选项
            #
            # # 显示菜单
            # rt.config(menu=menubar)
            # btinsert_stu = tk.Button(rt, text='录入记录', font=('微软雅黑', 9), command=add)
            # btinsert_stu.place(x=50, y=50, height=40, width=200)
            # btdelete_stu = tk.Button(rt, text='删除记录', font=('微软雅黑', 9), command=delete)
            # btdelete_stu.place(x=300, y=50, height=40, width=200)
            # btchange_stu = tk.Button(rt, text='修改记录', font=('微软雅黑', 9), command=change)
            # btchange_stu.place(x=50, y=200, height=40, width=200)
            # btselect_stu = tk.Button(rt, text='查询一个学生的记录', font=('微软雅黑', 9), command=quire)
            # btselect_stu.place(x=300, y=200, height=40, width=200)
            # rt.mainloop()

            #return 0
    '''cur.execute(sql)
    us=cur.fetchall()
    conn.commit()
    for x in us:
        if v1 == x[0]:
            sql = "select password from yh where user='" + x[0] + "'"
            cur.execute(sql)
            pw=cur.fetchall()
            if pw[0][0] == password:
                mb.showwarning('登录','登陆成功！')
                return 0
            else:
                mb.showerror('登录', '密码错误！')
                return 0'''

#注册功能
def zhuce():
    def pd():  # 注册的账户不能与数据库中的重复
        e11 = e1.get()
        e21 = e2.get()
        sql = 'select user from yh'
        cur.execute(sql)
        zh=cur.fetchall()
        if e11 == ' ':  # 用户名不为空
            yhm = mb.showwarning('注册', '用户名为空！')
            return 0
        for x in zh:
            if x[0] == e11:  # 用户名不重复
                yhm1 = mb.showerror('注册', '用户名已存在！')
                return 0
        if e21 == '':  # 密码不为空
            mm = mb.showwarning('注册', '密码为空！')
            return 0
        sql = "insert into yh(user,password) values('%s','%s')"%(e11, e21)
        cur.execute(sql)  # 添加到数据库
        conn.commit()
        mb.showwarning('注册','注册成功！')
        zc.destroy()
        zc.mainloop()
    zc = tk.Tk(className='注册')
    e1 = tk.Entry(zc)
    e2 = tk.Entry(zc)
    e1.place(x=50, y=10)
    e2.place(x=50, y=50)
    l1 = tk.Label(zc, text='用户名')
    l2 = tk.Label(zc, text='密码')
    l1.place(x=10, y=10)
    l2.place(x=10, y=50)
    b = tk.Button(zc, text='确定', command=pd)
    b.place(x=70, y=130)
    #zc.mainloop()
    return 0


root1=tk.Tk(className='学生成绩管理')
root1.geometry('550x350')
v1=tk.StringVar()
lb1=tk.Label(root1,text='账号：')
lb1.place(x=100,y=100,width=50,height=20)
e1=tk.Entry(root1,textvariable=v1)
e1.place(x=150,y=100,width=200,height=20)
lb2=tk.Label(root1,text='密码：')
lb2.place(x=100,y=160,width=50,height=20)
v2=tk.StringVar()
e2=tk.Entry(root1,textvariable=v2)
e2.place(x=150,y=160,width=200,height=20)
b1=tk.Button(root1,text='登 录',command=denglu)
b2=tk.Button(root1,text='注  册',command=zhuce)
b1.place(x = 200,y = 280)
b2.place(x = 250,y = 280)
root1.mainloop()

